<template>
	<view>
		<city-select :activeCity="activeCity" :locationCity="locationCity" :obtainCitys="obtainCitys" :formatName="formatName"
		 :hotCity="hotCity" :nearbyArea="nearbyArea" ref="city" @cityClick="selectCity"></city-select>
		 <loading-cover ref="loadingCover"></loading-cover>
	</view>
</template>

<script>
	import citySelect from "@/components/city-select/city-select.vue"
	import WxMap from 'common/js/map-wx-jssdk.js'

	export default {
		components: {
			citySelect
		},
		data() {
			return {
				activeCity: null,
				locationCity: null,
				obtainCitys: [],//全部城市/地区
				hotCity: [],//热门城市/地区
				nearbyArea:[],//附近地区
				formatName: 'title',
				cityAddon: 0,
				isLoad:false,
			}
		},
		onShow() {
			let that = this;
			// 刷新多语言
			this.$langConfig.refresh();
			this.getCityList();
			this.getLocation();
			this.checkAddonIsexit();
			if (uni.getStorageSync('city')) this.activeCity = uni.getStorageSync('city');
			this.getHotArea();
			this.getNearbyArea();
			setTimeout(function(){
				if (that.$refs.loadingCover) that.$refs.loadingCover.hide();
			}, 1000);
		},
		methods: {
			getCityList() {
				if (uni.getStorageSync('cityData')){
					this.obtainCitys = uni.getStorageSync('cityData');
				}else{
					this.$api.sendRequest({
						url: '/api/address/arealist',
						data:{
							level : '2,3',
						},
						success: res => {
							if (res.data) {
								this.obtainCitys = res.data;
								this.$refs.city.updateCitys(res.data);
								uni.setStorageSync('cityData', res.data);
							}
						}
					})
				}
			},
			//获取热门地区
			getHotArea(){
				this.$api.sendRequest({
					url: '/api/address/hotArea',
					success: res => {
						if (res.code >= 0) {
							let hotCity = [];
							hotCity.push({id : 0, title : '全国'});
							for(let i in res.data){
								let item = res.data[i];
								item.title = item.name;
								hotCity.push(item);
							}
							this.hotCity = hotCity;
						}
					}
				})
			},
			//获取附近城市
			getNearbyArea(){
				let activeCity = this.activeCity;
				let area_id = (activeCity && activeCity.id) ? activeCity.id : 0;
				if(area_id){
					this.$api.sendRequest({
						url: '/api/address/nearbyArea',
						data:{
							'area_id' : area_id
						},
						success: res => {
							if (res.code >= 0) {
								this.nearbyArea = res.data;
							}
						}
					})
				}
				
			},
			selectCity(data) {
				uni.setStorage({
					key: 'city',
					data: data,
					success: () => {
						// if (this.cityAddon == 1) uni.navigateBack();
						this.$util.goBack();
					}
				})
			},
			/**
			 * 获取定位城市
			 */
			getLocation() {
				const map = new WxMap({
					key: this.$config.mpKey
				});
				uni.getLocation({
					type: 'gcj02',
					success: (res) => {
						map.reverseGeocoder({
							location: {
								latitude: res.latitude,
								longitude: res.longitude
							},
							success: res => {
								this.$api.sendRequest({
									url: '/api/address/citybyname',
									data: {
										city: res.result.address_component.city
									},
									success: res => {
										if (res.data) {
											this.locationCity = res.data;
										}
									}
								})
							},
							fail: res => {
								console.error('定位失败错误信息：' + res.message)
							}
						})
					}
				})
			},
			checkAddonIsexit() {
				this.$api.sendRequest({
					url: '/api/addon/isexit',
					data: {
						name: 'city'
					},
					success: res => {
						this.cityAddon = res.data;
					}
				})
			}
		}
	}
</script>

<style>
</style>
